package com.example.nearby;


import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Build;

public class NearbyController extends ApplicationActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		viewID = R.layout.activity_nearby_controller;
		setContentView(viewID);
		System.out.println("Testing...");
		Button button = (Button)findViewById(R.id.login_button);
		setButtonDestination(button, "login");
		button = (Button)findViewById(R.id.guest_button);
		setButtonDestination(button, "guest");
		button = (Button)findViewById(R.id.goto_storefront);
		setButtonDestination(button, "storefront");
		button = (Button)findViewById(R.id.view_store_button);
		setButtonDestination(button, "view");
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, getAllStorefrontNames());
        AutoCompleteTextView textView = (AutoCompleteTextView)
                findViewById(R.id.search_storefront);
        textView.setAdapter(adapter);
        /*textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				AutoCompleteTextView textView = (AutoCompleteTextView)
		                findViewById(R.id.search_storefront);
				int id = getStoreIDFromName(textView.getText().toString());
				if(id != -1) {
					SessionManager session = SessionManager.getSession();
					session.addMsg("view_store_id", id + "");
					gotoActivity("view");
				}
			}
        	
        });*/
		boolean session = SessionManager.getSession() != null;
		if(session) {
			LinearLayout view = (LinearLayout)findViewById(R.id.authentication_items);
			view.setVisibility(View.GONE);
			TextView text = (TextView)findViewById(R.id.home_welcome);
			String name = SessionManager.getSession().getUsername();
			text.setText("Welcome " + name + "!");
		} else {
			LinearLayout view = (LinearLayout)findViewById(R.id.action_items);
			view.setVisibility(View.GONE);
		}
	}
	
	public boolean doBeforeFilter(Button source) {
		if(source.getId() == R.id.view_store_button) {
			AutoCompleteTextView textView = (AutoCompleteTextView)
	                findViewById(R.id.search_storefront);
			String value = textView.getText().toString();
			int id = getStoreIDFromName(value);
			if(id != -1) {
				System.out.println("Succeeded at " + value);
				SessionManager session = SessionManager.getSession();
				session.addMsg("view_store_id", id + "");
				return true;
			} else {
				System.out.println("Failed at " + value);
				return true;
			}
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.nearby_controller, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.fragment_nearby_controller, container, false);
			return rootView;
		}
	}

}
